package ru.amse.tsyganov.jumleditor.commands;

import ru.amse.tsyganov.jumleditor.model.Transition;
import ru.amse.tsyganov.jumleditor.view.TransitionView;

/*package*/ class DeleteTransitionCommand<T extends Transition> extends Command {
	
	private final TransitionView<T> transitionView;
	
	private final Transition transition;
	
	public DeleteTransitionCommand(
			TransitionView<T> transitionView) {
		
		if (transitionView == null) {
			throw new IllegalArgumentException();
		}
		
		this.transition = transitionView.getModel();
		this.transitionView = transitionView;
	}
	
	@Override
	public void execute() {
		
		transitionView.getModelViewMap().getTransitionsMap().
			remove(transition);
		
		transitionView.getSelectedItems().
			selectedTransitionsRemove(transitionView);
		
		Transition t = transitionView.getModel();
		t.getSource().removeOutgoingTransition(t);
		t.getTarget().removeIncomingTransition(t);	
	}

	@Override
	public void unexecute() {
		
		transitionView.getModelViewMap().getTransitionsMap().
			put(transition, transitionView);
		
		Transition t = transitionView.getModel();
		t.getSource().addOutgoingTransition(t);
		t.getTarget().addIncomingTransition(t);
	}
}
